using System.Threading;
using Charisma.Tests.Models.BlogApp;
using Charisma.Tests.Models.Fake;
using NUnit.Framework;

namespace Charisma.Tests
{
  [TestFixture]
  public class DbContextTests
  {
    [Test]
    public void InitializeDefaultRepositoriesTest()
    {
      var context = new BlogAppContext();

      Assert.IsNotNull(context.Blogs);
      Assert.IsNotNull(context.Persons);
    }

    [Test]
    public void InitializeArbitraryRepositoriesTest()
    {
      var context = new FakeContext();

      Assert.IsNotNull(context.FakeClassRepository);
      Assert.IsNotNull(context.FakeGenericInterfaceRepository);
      Assert.IsNotNull(context.FakeNonGenericInterfaceRepository);
      Assert.IsNotNull(context.GenericClassRepository);
      Assert.IsNotNull(context.GenericInterfaceRepository);
      Assert.IsNotNull(context.InstanceClassRepository);
      Assert.IsNull(context.NonGenericInterfaceRepository);
    }

    [Test]
    public void ParallelInitializeTest()
    {
      var t = new Thread(() => new BlogAppContext());
      t.Start();
      t.Join();
      var context = new BlogAppContext();
    }

    [Test]
    public void DisposeTest()
    {
      var context = new BlogAppContext();
      context.Dispose();
    }

    [Test]
    public void DisposeTwiceTest()
    {
      var context = new BlogAppContext();
      context.Dispose();
      context.Dispose();
    }

    [Test]
    public void DisposeFromAnotherThreadTest()
    {
      var context = new BlogAppContext();
      var t = new Thread(context.Dispose);
      t.Start();
      t.Join();
      context.Dispose();
    }
  }
}